package com.zxy.ziems.server.alarm.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.ziems.server.alarm.convertor.AlarmConvertor;
import com.zxy.ziems.server.alarm.mapper.AlarmRuleRangeMapper;
import com.zxy.ziems.server.alarm.pojo.entity.AlarmRuleRangeEntity;
import com.zxy.ziems.server.alarm.pojo.entity.AlarmTypeEntity;
import com.zxy.ziems.server.alarm.pojo.vo.AlarmRuleRangeVO;
import com.zxy.ziems.server.alarm.service.AlarmRuleRangeService;
import com.zxy.ziems.server.alarm.service.AlarmTypeService;
import com.zxy.ziems.server.device.pojo.entity.DeviceTypeEntity;
import com.zxy.ziems.server.device.service.DeviceTypeService;
import com.zxy.ziems.server.mybatis.core.service.ServiceImplX;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.common.quota.ClientQuotaAlteration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Copyright (C),
 * @ClassName: AlarmRuleRangeServiceImpl
 * @Author: yanzhiwei
 * @Description:
 * @Date: 2024/4/10 15:48
 */
@Service
@Slf4j
public class AlarmRuleRangeServiceImpl extends ServiceImplX<AlarmRuleRangeMapper, AlarmRuleRangeEntity> implements AlarmRuleRangeService {

    /**
     * 保存告警通知范围
     *
     * @param ruleId
     * @param alarmRuleRangeEntityList
     */
    @Override
    public void saveOrUpdateAlarmRuleRange(String ruleId, List<AlarmRuleRangeEntity> alarmRuleRangeEntityList) {
        // 删除以前
        deleteAlarmRuleRangeByRuleId(ruleId);

        for (AlarmRuleRangeEntity alarmRuleRangeEntity : alarmRuleRangeEntityList) {
            alarmRuleRangeEntity.setRuleId(ruleId);
        }
        this.saveBatch(alarmRuleRangeEntityList);
    }

    /**
     * 根据ruleId删除
     *
     * @param ruleId
     */
    @Override
    public void deleteAlarmRuleRangeByRuleId(String ruleId) {
        QueryWrapper<AlarmRuleRangeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AlarmRuleRangeEntity::getRuleId, ruleId);
        this.remove(queryWrapper);
    }

    @Override
    public List<AlarmRuleRangeVO> listByIdList(Set<String> ruleIdSet) {
        QueryWrapper<AlarmRuleRangeEntity> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().in(AlarmRuleRangeEntity::getRuleId, ruleIdSet);
        List<AlarmRuleRangeEntity> list = this.list(queryWrapper);

        Set<String> deviceTypeIdSet = new HashSet<>();
        Set<String> alarmTypeIdSet = new HashSet<>();
        for (AlarmRuleRangeEntity entity : list) {
            if (StrUtil.isNotBlank(entity.getDeviceTypeId())) {
                deviceTypeIdSet.add(entity.getDeviceTypeId());
            }
            if (StrUtil.isNotBlank(entity.getAlarmTypeId())) {
                alarmTypeIdSet.add(entity.getAlarmTypeId());
            }
        }

        Map<String, DeviceTypeEntity> deviceTypeEntityMap = deviceTypeService.listByIds(deviceTypeIdSet).stream()
                .collect(Collectors.toMap(DeviceTypeEntity::getId, (a) -> a));

        Map<String, AlarmTypeEntity> alarmTypeEntityMap = alarmTypeService.listByIds(alarmTypeIdSet).stream()
                .collect(Collectors.toMap(AlarmTypeEntity::getId, (a) -> a));

        List<AlarmRuleRangeVO> alarmRuleRoleVOList = list.stream()
                .map((entity) -> {
                    DeviceTypeEntity deviceTypeEntity = StrUtil.isBlank(entity.getDeviceTypeId()) ? null : deviceTypeEntityMap.get(entity.getDeviceTypeId());
                    AlarmTypeEntity alarmTypeEntity = StrUtil.isBlank(entity.getAlarmTypeId()) ? null : alarmTypeEntityMap.get(entity.getAlarmTypeId());

                    return AlarmConvertor.INSTANCE.alarmRuleTypeDOConvertor2VO(
                            entity,
                            deviceTypeEntity,
                            alarmTypeEntity
                    );
                }).toList();

        return alarmRuleRoleVOList;
    }

    @Override
    public List<AlarmRuleRangeEntity> listByAlarmType(String deviceTypeCode, String alarmTypeId) {
        LambdaQueryWrapper<AlarmRuleRangeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AlarmRuleRangeEntity::getDeviceTypeCode, deviceTypeCode)
                .eq(AlarmRuleRangeEntity::getAlarmTypeId, alarmTypeId);
        return baseMapper.selectList(wrapper);
    }

    private DeviceTypeService deviceTypeService;

    @Autowired
    public void setRoleService(DeviceTypeService deviceTypeService) {
        this.deviceTypeService = deviceTypeService;
    }

    private AlarmTypeService alarmTypeService;

    @Autowired
    public void setRoleService(AlarmTypeService alarmTypeService) {
        this.alarmTypeService = alarmTypeService;
    }
}

